package com.xxh.myapp.controller;



import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xxh.myapp.entity.Course;
import com.xxh.myapp.entity.Score;
import com.xxh.myapp.entity.wrapper.ScoreWrapper;
import com.xxh.myapp.msg.ReturnMsg;
import com.xxh.myapp.service.CourseService;
import com.xxh.myapp.service.ScoreService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * (Score)表控制层
 *
 * @author makejava
 * @since 2020-05-01 16:37:44
 */
@RestController
@RequestMapping("score")
public class ScoreController extends ApiController {
    /**
     * 服务对象
     */
    @Resource
    private ScoreService scoreService;

    @Resource
    private CourseService courseService;

    /**
     * 分页查询所有数据
     *
     * @param page 分页对象
     * @param score 查询实体
     * @return 所有数据
     */
    @GetMapping
    public R selectAll(Page<Score> page, Score score) {
        return success(this.scoreService.page(page, new QueryWrapper<>(score)));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public R selectOne(@PathVariable Serializable id) {
        return success(this.scoreService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param score 实体对象
     * @return 新增结果
     */
    @PostMapping
    public R insert(@RequestBody Score score) {
        return success(this.scoreService.save(score));
    }

    /**
     * 修改数据
     *
     * @param score 实体对象
     * @return 修改结果
     */
    @PutMapping
    public R update(@RequestBody Score score) {
        return success(this.scoreService.updateById(score));
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping
    public R delete(@RequestParam("idList") List<Long> idList) {
        return success(this.scoreService.removeByIds(idList));
    }

    //根据学生的sno拿到成绩(学生查询自己的成绩)
    @GetMapping("getScoreBySno")
    public Map<String, List<ScoreWrapper>>getScoreBySno(int sno){

        QueryWrapper<Score> scoreQueryWrapper = new QueryWrapper<>();
        //查询构造器。
        scoreQueryWrapper.eq("sno",sno);
        scoreQueryWrapper.ne("grade",-1);
        //拿到学生的成绩信息
        List<Score> scores = scoreService.list(scoreQueryWrapper);
        ArrayList<Integer> cnolist = new ArrayList<>();
        for(Score s:scores){
            cnolist.add(s.getCno());
        }
        //拿到课程信息
        List<Course> courses = courseService.listByIds(cnolist);
        //丰富信息
        ArrayList<ScoreWrapper> scoreWrappers = new ArrayList<>();
        for(int i=0;i<scores.size();i++){
            ScoreWrapper scoreWrapper = new ScoreWrapper();
            scoreWrapper.setScore(scores.get(i));
            scoreWrapper.setCourseName(courses.get(i).getCname());
            scoreWrappers.add(scoreWrapper);
        }
        Map<String, List<ScoreWrapper>> map = new HashMap<>();
        map.put("myscore",scoreWrappers);
        return map;
    }

    //老师登分的接口
    @PutMapping("insertIntoScore")
    public ReturnMsg insertIntoScore(int sno,int cno,int grade){
        Score score = new Score();
        score.setCno(cno);
        score.setSno(sno);
        score.setGrade(grade);
        scoreService.save(score);
        ReturnMsg returnMsg = new ReturnMsg();
        returnMsg.setCode(200);
        returnMsg.setMsg("登分成功");
        return returnMsg;
    }



}